/**
 * Created by tangtao on 2017/04/25.
 */
(function () {
    'use strict';

    angular.module('App.pages.member')
        .controller('AssessDetailCtrl', AssessDetailCtrl);

    function AssessDetailCtrl(MemberModel, $scope,$ionicSlideBoxDelegate, $ionicPopup,AppUtils,$state) {

        var vm = this;
        vm.indexComment = null;
        vm.Assesslength=[];
        vm.assess={};
        vm.assessVo = {};
        vm.assessDetail = null;
        vm.AsAndMeVo = {};

        vm.test=function () {
            MemberModel.findAssess().$promise.then(function (response) {
                if (response.assess === '2') {
                    $state.go('memberMenu.assessReport',{memberType:"2"});
                } else {
                    $ionicSlideBoxDelegate.update();
                    console.log(response.assess.length);
                    vm.Assesslength = response.assess.length;
                    vm.assess = response.assess;
                }
            })
        }
        $scope.buttonas = function () {
            var attr = angular.element("#chevron-button").attr("class");
            if (attr == 'icon ion-chevron-right activated') {
                angular.element("#chevron-button").attr("class","icon ion-chevron-left");
                angular.element(".rightMenu").css("width","0%");
                angular.element(".rightFixBox").css("width","20px");
            } else {
                angular.element("#chevron-button").attr("class","icon ion-chevron-right");
                angular.element(".rightMenu").css("width","55%");
                angular.element(".rightFixBox").css("width","60%");
                MemberModel.getMemberList().$promise.then(function (response) {
                    $scope.memberList=response.memberList;
                    console.log(response+"=============")
                })
            }
        }
        // $scope.search = function () {
        //     var objParams = {};
        //     var searchName = angular.element(".searchName").val();
        //     var reg = /^[\u4e00-\u9fa5]+$/i;
        //     objParams.searchName = searchName;
        //     if (reg.test(searchName)) {
        //         MemberModel.getStudentName(objParams).$promise.then(function (response) {
        //             $scope.memberList=response.studentName;
        //         })
        //     } else if(searchName == null) {
        //         MemberModel.getMemberList().$promise.then(function (response) {
        //             $scope.memberList=response.memberList;
        //             console.log(response+"=============")
        //         })
        //     }
        // }
        $scope.goDetail = function (id) {
            var parms = {};
            parms.memberId = id;
            MemberModel.getAssessById(parms).$promise.then(function (response) {
                AppUtils.scrollTop();
                $ionicSlideBoxDelegate.slide(0, [100]);
                angular.element(".as-one").css("display","none");
                angular.element(".as-two").css("display","block");
                angular.element(".as-two").css("visibility","visible");
                angular.element(".show-index").css("display","none");
                vm.assessDetail = response.assess;
                vm.emotion = response.assess.emotion;
                vm.communication = response.assess.communication;
                vm.operation = response.assess.operation;
                vm.habit = response.assess.habit;
                vm.hygiene = response.assess.hygiene;
                vm.theMeal = response.assess.theMeal;
                vm.sinceReason = response.assess.sinceReason;
                vm.healthy = response.assess.healthy;
                vm.sleep = response.assess.sleep;
                vm.memberId = response.assess.memberId;
                vm.message = response.assess.message;
                vm.assessStatus = response.assess.assessStatus;
                vm.assessId = response.assess.assessId;
            })
        }

        // $scope.test = function (obj){
        //     var esdw = obj.target;
        //     alert(esdw.getAttribute("name"));
        // }


        $scope.ckLabel = function (obj) {

            var esdw = obj.target;
            var elem = esdw.getAttribute("name");
            var value = esdw.getAttribute("value");
            // alert(angular.element(value).attr(value));
            var emotion = "emotion";
            var communication = "communication";
            var operation = "operation";
            var habit = "habit";
            var hygiene = "hygiene";
            var theMeal = "theMeal";
            var sinceReason = "sinceReason";
            var healthy = "healthy";
            var sleep = "sleep";
            switch (elem) {
                case emotion:
                    vm.emotion=value;
                    break;
                case communication:
                    vm.communication = value;
                    break;
                case operation:
                    vm.operation = value;
                    break;
                case habit:
                    vm.habit = value;
                    break;
                case hygiene:
                    vm.hygiene = value;
                    break;
                case theMeal:
                    vm.theMeal = value;
                    break;
                case sinceReason:
                    vm.sinceReason = value;
                    break;
                case healthy:
                    vm.healthy = value;
                    break;
                case sleep:
                    vm.sleep = value;
                    break;
            }
        }

        $scope.saveAssess = function (index) {
            var size = 0;
            // alert($scope.s.id);
            vm.assessVo.id = vm.assess[this.$index].id;
            vm.assessVo.emotion = vm.assess[this.$index].emotion;
            vm.assessVo.communication = vm.assess[this.$index].communication;
            vm.assessVo.operation = vm.assess[this.$index].operation;
            vm.assessVo.habit = vm.assess[this.$index].habit;
            vm.assessVo.hygiene = vm.assess[this.$index].hygiene;
            vm.assessVo.theMeal = vm.assess[this.$index].theMeal;
            vm.assessVo.sinceReason = vm.assess[this.$index].sinceReason;
            vm.assessVo.healthy = vm.assess[this.$index].healthy;
            vm.assessVo.sleep = vm.assess[this.$index].sleep;
            vm.assessVo.message = vm.assess[this.$index].message;
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.emotion)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.communication)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.operation)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.habit)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.hygiene)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.theMeal)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.sinceReason)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.healthy)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.sleep)) {
                size=1;
            }
            if (!AppUtils.isUndefinedOrNull(vm.assessVo.message)) {
                size=1;
            }

            if (size == 0) {
                var alertPopup  = $ionicPopup.alert({
                    title: '提示',
                    template: '您没进行任何评分，不需要保存'
                });
                return;
            }

            MemberModel.saveAssess(vm.assessVo).$promise.then(function (response) {
                if (response.msg == 'success' ) {
                    var alertPopup  = $ionicPopup.alert({
                        title: '提示',
                        template: '保存成功'
                    });
                    // if ((index+1) == vm.Assesslength) {
                    //     return;
                    // }
                    alertPopup.then(function(res) {
                        if ((index+1) == vm.Assesslength) {
                            return;
                        }
                        $ionicSlideBoxDelegate.slide(index+1);
                    });
                }
            });
        }

        $scope.ckLabelOne = function (obj) {
            var esdw = obj.target;
            var elem = esdw.getAttribute("name");
            var value = esdw.getAttribute("value");
            var emotion = "emotion";
            var communication = "communication";
            var operation = "operation";
            var habit = "habit";
            var hygiene = "hygiene";
            var theMeal = "theMeal";
            var sinceReason = "sinceReason";
            var healthy = "healthy";
            var sleep = "sleep";
            switch (elem) {
                case emotion:
                    vm.assess[this.$index].emotion=value;
                    break;
                case communication:
                    vm.assess[this.$index].communication = value;
                    break;
                case operation:
                    vm.assess[this.$index].operation = value;
                    break;
                case habit:
                    vm.assess[this.$index].habit = value;
                    break;
                case hygiene:
                    vm.assess[this.$index].hygiene = value;
                    break;
                case theMeal:
                    vm.assess[this.$index].theMeal = value;
                    break;
                case sinceReason:
                    vm.assess[this.$index].sinceReason = value;
                    break;
                case healthy:
                    vm.assess[this.$index].healthy = value;
                    break;
                case sleep:
                    vm.assess[this.$index].sleep = value;
                    break;
            }
        }
        $scope.saveAssessTwo = function () {

            vm.AsAndMeVo.memberId = vm.memberId;
            vm.AsAndMeVo.assessId = vm.assessId;
            vm.AsAndMeVo.emotion = vm.assessDetail.emotion;
            vm.AsAndMeVo.communication = vm.assessDetail.communication;
            vm.AsAndMeVo.operation = vm.assessDetail.operation;
            vm.AsAndMeVo.habit = vm.assessDetail.habit;
            vm.AsAndMeVo.hygiene = vm.assessDetail.hygiene;
            vm.AsAndMeVo.theMeal = vm.assessDetail.theMeal;
            vm.AsAndMeVo.sinceReason = vm.assessDetail.sinceReason;
            vm.AsAndMeVo.healthy = vm.assessDetail.healthy;
            vm.AsAndMeVo.sleep = vm.assessDetail.sleep;
            vm.AsAndMeVo.message = vm.assessDetail.message;
                MemberModel.saveAssessTwo(vm.AsAndMeVo).$promise.then(function (res) {
                    if (response.msg == 'success' ) {
                        var alertPopup  = $ionicPopup.alert({
                            title: '提示',
                            template: '保存成功'
                        });
                    }
                });
            }
        $scope.colsethis = function () {
            angular.element(".as-one").css("display","block");
            angular.element(".as-two").css("display","none");
            angular.element(".show-index").css("display","block");
        }

        vm.test();

        /**
         * ===================================改版js===========================================================================
         */
        $scope.ratingFull = {};
        // $scope.ratingFull.rate = 3;
        $scope.ratingFull.max = 5;
        $scope.reportEntrance = function (id) {
            $state.go('memberMenu.assessReport',{memberId:id});
        }
        $scope.commentEdit = function (index) {
            vm.indexComment = index;
            angular.element("#dynamic-comment-box").css("display","block");
        }
        $scope.cancelComment = function () {
            angular.element("#dynamic-comment-box").css("display","none");
        }
        $scope.commitComment = function () {
            // alert($ionicslideboxdelegate.currentIndex());
            alert(vm.indexComment);
            vm.assess[vm.indexComment].message = angular.element("textarea").val();
            angular.element("#dynamic-comment-box").css("display","none");
        }
        $scope.colseTwo = function () {
            var alertPopup = $ionicPopup.alert({
                title: '提示',
                template: '返回滑动列表界面'
            });
            alertPopup.then(function () {
                angular.element(".as-one").css("display","block");
                angular.element(".as-two").css("display","none");
                angular.element(".show-index").css("display","block");
            });
            // alert("");
        }
    }
})();